let express = require("express")();
let expressStatic = require("express-static");
let mysql = require("mysql");
const { error } = require("console");

const port = 82;

let sql = mysql.createConnection({
    host      : 'localhost',
    user      : 'root',
    password  : '123456',
    database  : 'realme'
});

sql.connect();  

express.get("/getPhoneList",(request,response)=>{
    sql.query(`select * from goods`,(error,data)=>{
        if(error){
            response.send("error")
        }
        else{
            response.send(data)
        }
    })
})


express.get("/getPhoneDetails",(request,response)=>{
    let id = request.query.id;
	sql.query(`SELECT * FROM goods WHERE id=${id}`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}
		else{
			response.send(data[0])
		}
	})
    
})



express.get("/getShopcar",(request,response)=>{
    let text = request.query;
	sql.query(`select orders.userid,orders.goodsid,orders.count,goods.id,goods.title,goods.banben,goods.color,goods.price1,goods.img from orders,goods where orders.userid= "${text.id}" and goods.id=orders.goodsid`,(error,data)=>{
		
		if(error){
			console.log(error);
			response.send("error");
		}
		else{
			response.send(data)
		}
	})  
})


express.get("/delorder",(request,response)=>{
    let d = request.query;
	sql.query(`delete from orders WHERE userid=${d.userid}`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}
		else{
			response.send("success")
		}
	})
    
})



express.get("/addorder",(request,response)=>{
    let a = request.query;
	sql.query(`insert into dingdan (title,color,banben,userid,count,address,phonenumber,name) values ("${a.title}","${a.color}","${a.banben}","${a.userid}","${a.count}","${a.address}","${a.phonenumber}","${a.name}")`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}
		else{
			response.send("success")
		}
	})
    
})


express.get("/addShopcar",(request,response)=>{
	let w = request.query;
	let n = Number(w.userid);
	console.log(n)
	sql.query(`select * from orders where userid = "${w.userid}"`,(error,data)=>{
		if(error){
			response.send("error")
			console.log(error)
		}
		else{
			if(data.length === 0){
				console.log("是我")
				sql.query(`insert into orders (goodsid,userid) values ("${w.goodsid}","${w.userid}")`,(error,data)=>{
					if(error){
						response.send("error")
						console.log(error)
					}
					else{
						response.send(data)
					}
				})
			}
			else{
				if(data[0].userid === n){
					sql.query(`update orders set goodsid = "${w.goodsid}" where userid = "${w.userid}"`,(error,data)=>{
						if(error){
							response.send("error")
						}
						else{
							response.send(data)
						}
					})
				}	
			}
			
		}
	})
})


express.get("/count",(request,response)=>{
	let c = request.query; 
	sql.query(`update orders set count = "${c.count}" where userid = "${c.userid}"`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}
		else{
            response.send("data")
		}
	}) 
})




express.get("/login",(request,response)=>{
    let q = request.query; 
	sql.query(`SELECT * FROM users WHERE username="${q.username}" AND password="${q.password}"`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("3");
		}
		else{
            if(data.length === 0){
                response.send("2")
            }
            else{
                response.send(JSON.stringify(data))
            }	
            console.log(data)
		}
	}) 
})


express.get("/resigest",(request,response)=>{
	let p = request.query; 
	console.log(p)
	sql.query(`insert into users (username,password) values ("${p.username}","${p.password}")`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("3");
		}
		else{
            response.send("success")
            console.log(data)
		}
	}) 
})



express.use(expressStatic(__dirname + '/static'));

express.listen(port);
console.log(`express is running at ${port}`)